# -*- coding: utf-8 -*-
# @Time    : 2021/5/27 21:28
# @Author  : YIGE
# @Email   : yige@maqu.com
# @File    : resFormatUtil.py
# @Software: PyCharm

from django.http import JsonResponse

"""
反射：python的反射本质就是利用字符串操作对象的成员
"""


class ResFormat:
    SUCCESS = 200, '成功'

    PARAM_ERROR = 4001, '参数错误'
    ACCESS_RESTRICTION = 4002, '访问限制'
    ACCOUNT_REGISTERED = 4003, '已被注册'
    OVERDUE_ABNORMAL = 4004, '过期'
    UPLOAD_FAIL = 4011, '文件上传失败'

    UNKNOWN_ERROR = 4100, '未知错误'

    def __init__(self, status='SUCCESS', data=''):
        """
        初始化
        默认状态为成功响应状态，返回数据为空
        :param status: 响应状态
        :param data: 响应数据
        """

        # 校验该状态是否存在
        if hasattr(self, status):
            status = getattr(self, status)
        else:
            status = self.UNKNOWN_ERROR

        code, msg = status

        self.res = {
            'code': code,
            'msg': msg,
            'data': data
        }

    def json_response(self):
        """
        返回转义后的JSON数据的JSON响应对象
        :return: JsonResponse
        """
        return JsonResponse(self.res, json_dumps_params={'ensure_ascii': False})
